﻿using Volo.Abp.Auditing;

namespace PLCS.Domain;

public class BaseEntity : Entity<Guid>, IGeneratesDomainEvents, IHasCreationTime, IHasModificationTime
{
    public override Guid Id { get; protected set; } = Guid.NewGuid();
    public DateTime? LastModificationTime { get; set; }
    public DateTime CreationTime { get; set; }

    [NotMapped]
    private readonly ICollection<DomainEventRecord> _distributedEvents = new Collection<DomainEventRecord>();

    [NotMapped]
    private readonly ICollection<DomainEventRecord> _localEvents = new Collection<DomainEventRecord>();

    public void ClearDistributedEvents()
    {
        _distributedEvents.Clear();
    }

    public void ClearLocalEvents()
    {
        _localEvents.Clear();
    }

    protected virtual void AddLocalEvent(object eventData)
    {
        _localEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
    }

    protected virtual void AddDistributedEvent(object eventData)
    {
        _distributedEvents.Add(new DomainEventRecord(eventData, EventOrderGenerator.GetNext()));
    }

    public IEnumerable<DomainEventRecord> GetDistributedEvents()
    {
        return _distributedEvents;
    }

    public IEnumerable<DomainEventRecord> GetLocalEvents()
    {
        return _localEvents;
    }
}